package com.xnyzc.lhy.order.service.impl.vehicle;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.penum.vehicle.EVehicleStatus;
import com.xnyzc.lhy.order.entity.gps.OaDVehicleGps;
import com.xnyzc.lhy.order.entity.vehicle.OaDDriverVehicle;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.order.mapper.gps.OaDVehicleGpsMapper;
import com.xnyzc.lhy.order.mapper.vehicle.OaDDriverVehicleMapper;
import com.xnyzc.lhy.order.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.order.service.vehicle.VehicleIntegrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息
 */
@Service
public class VehicleIntegrationServiceImpl implements VehicleIntegrationService {

    @Autowired
    private OaDDriverVehicleMapper oaDDriverVehicleMapper;
    @Autowired
    private OaSysVehicleMapper oaSysVehicleMapper;
    @Autowired
    private OaDVehicleGpsMapper oaDVehicleGpsMapper;
    @Autowired
    private RedisCommon redisCommon;

    private static final String REDIS_KEY_PREFIX = "gps:oa_d_vehicle_gps:";

    /**
     * 获取车辆信息
     * @param driverId 司机id
*      测试车辆id 639370087071744
     * @return
     */
    @Override
    public Map<String, Object> getVehicleIntegration(Long driverId){
        Map<String, Object> map = new HashMap<>(16);
        // 司机车辆关联表
        OaDDriverVehicle oaDDriverVehicle = oaDDriverVehicleMapper.selectOne(
                Wrappers.<OaDDriverVehicle>lambdaQuery()
                        .eq(OaDDriverVehicle::getDriverId, driverId)
        );
        if (oaDDriverVehicle == null){
            return map;
        }
        // 车辆信息表
        OaSysVehicle oaSysVehicle = oaSysVehicleMapper.selectOne(
                Wrappers.<OaSysVehicle>lambdaQuery()
                        .eq(OaSysVehicle::getVehicleId, oaDDriverVehicle.getVehicleId())
        );
        if (oaSysVehicle == null){
            return map;
        }
        Long vehicleId = oaSysVehicle.getVehicleId();
        Integer vehStatus = oaSysVehicle.getVehStatus();
        map.put("vehNo", oaSysVehicle.getVehNo());
        map.put("vehicleId", vehicleId);
        map.put("vehStatus", vehStatus);
        if (vehStatus == null){
            map.put("vehStatusMsg", "");
        } else {
            map.put("vehStatusMsg", EVehicleStatus.findById(oaSysVehicle.getVehStatus()).getValue());
        }
        map.put("lng", "");
        map.put("lat", "");
        // 获取车辆经纬度
        String jsonStr = redisCommon.getStr(String.format("%s%s", REDIS_KEY_PREFIX, vehicleId));
        if (StringUtils.isEmpty(jsonStr)){
            // 未获取到redis数据
            List<OaDVehicleGps> oaDVehicleGpsList = oaDVehicleGpsMapper.selectList(
                    Wrappers.<OaDVehicleGps>lambdaQuery()
                            .eq(OaDVehicleGps::getDriverId, driverId)
                            .eq(OaDVehicleGps::getVehId, vehicleId)
                            .orderByDesc(OaDVehicleGps::getUpdateTime)
            );
            if (oaDVehicleGpsList.isEmpty()){
                return map;
            }
            OaDVehicleGps oaDVehicleGps = oaDVehicleGpsList.get(0);
            map.put("lng", oaDVehicleGps.getLng());
            map.put("lat", oaDVehicleGps.getLat());
        }
        OaDVehicleGps oaDVehicleGps = JSON.parseObject(jsonStr, OaDVehicleGps.class);
        map.put("lng", oaDVehicleGps.getLng());
        map.put("lat", oaDVehicleGps.getLat());
        return map;
    }
}
